# Link
# En: https://leetcode.com/problems/maximum-depth-of-binary-tree/
# 中: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

# 递归
class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        else:
            left_height = self.maxDepth(root.left)
            right_height = self.maxDepth(root.right)
            return max(left_height, right_height) + 1

# 迭代
# class Solution:
#     def maxDepth(self, root):
#         """
#         :type root: TreeNode
#         :rtype: int
#         """
#         stack = []
#         if root is not None:
#             stack.append((1, root))
#
#         depth = 0
#         while stack!= []:
#             current_depth, root = stack.pop()
#             if root is not None:
#                 depth = max(current_depth, depth)
#                 stack.append((current_depth+1, root.left))
#                 stack.append((current_depth+1, root.right))
#         return depth